/**
 * @Description: 路由配置文件 - 定义应用程序的路由规则和导航守卫
 * @Author: lujq
 * @Date: 2024-12-21
 * @LastEditors: lujq
 * @LastEditTime: 2025-08-25 14:00:00
 */
import { createRouter, createWebHistory } from 'vue-router'
import MainLayout from '@/layouts/MainLayout.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login/index.vue'),
      meta: {
        title: '用户登录',
        requiresAuth: false,
      },
    },
    {
      path: '/',
      redirect: '/dashboard',
    },
    {
      path: '/',
      component: MainLayout,
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('@/views/dashboard/index.vue'),
          meta: {
            title: '数据概览',
            icon: 'Odometer',
            showInMenu: true,
            menuOrder: 1,
          },
        },
        {
          path: 'customer/list',
          name: 'CustomerList',
          component: () => import('@/views/customer/list.vue'),
          meta: {
            title: '客户列表',
            icon: 'List',
            showInMenu: true,
            menuGroup: 'customer',
            menuGroupTitle: '客户管理',
            menuGroupIcon: 'User',
            menuOrder: 2,
          },
        },
        {
          path: 'customer/profile',
          name: 'CustomerProfile',
          component: () => import('@/views/customer/profile.vue'),
          meta: {
            title: '客户画像分析',
            icon: 'TrendCharts',
            showInMenu: true,
            menuGroup: 'customer',
            menuGroupTitle: '客户管理',
            menuGroupIcon: 'User',
            menuOrder: 2,
          },
        },
        {
          path: 'order/list',
          name: 'OrderList',
          component: () => import('@/views/order/list.vue'),
          meta: {
            title: '订单列表',
            icon: 'List',
            showInMenu: true,
            menuGroup: 'order',
            menuGroupTitle: '订单管理',
            menuGroupIcon: 'Document',
            menuOrder: 3,
          },
        },
        {
          path: 'order/statistics',
          name: 'OrderStatistics',
          component: () => import('@/views/order/statistics.vue'),
          meta: {
            title: '订单统计',
            icon: 'DataAnalysis',
            showInMenu: true,
            menuGroup: 'order',
            menuGroupTitle: '订单管理',
            menuGroupIcon: 'Document',
            menuOrder: 3,
          },
        },
        {
          path: 'analytics/sales',
          name: 'SalesAnalytics',
          component: () => import('@/views/analytics/sales.vue'),
          meta: {
            title: '销售分析',
            icon: 'TrendCharts',
            showInMenu: true,
            menuGroup: 'analytics',
            menuGroupTitle: '数据分析',
            menuGroupIcon: 'PieChart',
            menuOrder: 4,
          },
        },
        {
          path: 'analytics/customer',
          name: 'AnalyticsCustomer',
          component: () => import('@/views/analytics/customer.vue'),
          meta: {
            title: '客户分析',
            icon: 'UserFilled',
            showInMenu: true,
            menuGroup: 'analytics',
            menuGroupTitle: '数据分析',
            menuGroupIcon: 'PieChart',
            menuOrder: 4,
          },
        },
        {
          path: 'analytics/product',
          name: 'ProductAnalytics',
          component: () => import('@/views/analytics/product.vue'),
          meta: {
            title: '产品分析',
            icon: 'Goods',
            showInMenu: true,
            menuGroup: 'analytics',
            menuGroupTitle: '数据分析',
            menuGroupIcon: 'PieChart',
            menuOrder: 4,
          },
        },
        // 新增：综合数据统计页（按月份/季度统计客户新增与订单金额）
        {
          path: 'analytics/statistics',
          name: 'AnalyticsStatistics',
          component: () => import('@/views/analytics/statistics.vue'),
          meta: {
            title: '综合统计',
            icon: 'DataAnalysis',
            showInMenu: true,
            menuGroup: 'analytics',
            menuGroupTitle: '数据分析',
            menuGroupIcon: 'PieChart',
            menuOrder: 4,
          },
        },
        // 新增：业务员管理页（查看各业务员的客户跟进情况、订单完成量等数据）
        {
          path: 'sales/management',
          name: 'SalesManagement',
          component: () => import('@/views/sales/management.vue'),
          meta: {
            title: '业务员管理',
            icon: 'Management',
            showInMenu: true,
            menuGroup: 'sales',
            menuGroupTitle: '销售管理',
            menuGroupIcon: 'UserFilled',
            menuOrder: 5,
          },
        },
        // 新增：货款结算页（查看订单付款与收款记录、欠款标记）
        {
          path: 'finance/settlement',
          name: 'FinanceSettlement',
          component: () => import('@/views/finance/settlement.vue'),
          meta: {
            title: '货款结算',
            icon: 'Wallet',
            showInMenu: true,
            menuGroup: 'finance',
            menuGroupTitle: '财务管理',
            menuGroupIcon: 'Wallet',
            menuOrder: 6,
          },
        },
        {
          path: 'product/list',
          name: 'ProductList',
          component: () => import('@/views/product/list.vue'),
          meta: {
            title: '产品列表',
            icon: 'List',
            showInMenu: true,
            menuGroup: 'product',
            menuGroupTitle: '产品管理',
            menuGroupIcon: 'Goods',
            menuOrder: 5,
          },
        },
        {
          path: 'profile',
          name: 'Profile',
          component: () => import('@/views/profile/index.vue'),
          meta: {
            title: '个人中心',
            icon: 'User',
            showInMenu: false,
            menuOrder: 6,
          },
        },
        {
          path: 'users/list',
          name: 'UserList',
          component: () => import('@/views/users/list.vue'),
          meta: {
            title: '用户管理',
            icon: 'User',
            showInMenu: true,
            menuOrder: 6,
          },
        },
        {
          path: 'settings',
          name: 'Settings',
          component: () => import('@/views/settings/index.vue'),
          meta: {
            title: '系统设置',
            icon: 'Setting',
            showInMenu: true,
            menuOrder: 7,
          },
        },
      ],
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: () => import('@/views/404.vue'),
    },
  ],
})

/**
 * 检查用户是否已登录
 * @returns {boolean} 登录状态
 */
const isAuthenticated = (): boolean => {
  return localStorage.getItem('isLoggedIn') === 'true'
}

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta?.title) {
    document.title = `${to.meta.title} - 布匹销售系统`
  } else {
    document.title = '布匹销售系统'
  }

  // 检查是否需要认证
  const requiresAuth = to.meta?.requiresAuth !== false // 默认需要认证
  const isLoggedIn = isAuthenticated()

  if (requiresAuth && !isLoggedIn) {
    // 需要认证但未登录，跳转到登录页
    next('/login')
  } else if (to.path === '/login' && isLoggedIn) {
    // 已登录用户访问登录页，跳转到首页
    next('/dashboard')
  } else {
    // 正常访问
    next()
  }
})

export default router
